package springboot.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import springboot.demo.annotation.NotProfile;
import springboot.demo.bean.User;
import springboot.demo.constant.UserConstant;
import springboot.demo.dao.UserMapper;
import springboot.demo.service.UserService;

/**
 * User服务实现类
 */
@Service
@NotProfile("simple")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 用户注册
     * @param user 用户信息
     * @return 注册结果
     */
    @Override
    public boolean register(User user) {
        // 参数校验
        if (user == null || !StringUtils.hasText(user.getLoginname()) || !StringUtils.hasText(user.getPassword())) {
            return false;
        }
        
        // 检查用户名是否已存在
        if (getUserByLoginname(user.getLoginname()) != null) {
            return false;
        }
        
        // 设置默认值
        if (!StringUtils.hasText(user.getType())) {
            user.setType(UserConstant.UserType.USER); // 默认用户类型
        }
        if (user.getVer() == null) {
            user.setVer(UserConstant.Version.INITIAL); // 默认版本号
        }
        
        // 保存用户
        return this.save(user);
    }

    @Override
    public IPage<User> getUserPage(int page, int size) {
        Page<User> pageParam = new Page<>(page, size);
        return this.page(pageParam);
    }

    @Override
    public IPage<User> getUserPageByCondition(int page, int size, User user) {
        Page<User> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (user != null) {
            if (StringUtils.hasText(user.getLoginname())) {
                queryWrapper.like(User::getLoginname, user.getLoginname());
            }
            if (StringUtils.hasText(user.getRealname())) {
                queryWrapper.like(User::getRealname, user.getRealname());
            }
            if (StringUtils.hasText(user.getType())) {
                queryWrapper.eq(User::getType, user.getType());
            }
        }
        
        // 按id降序排序
        queryWrapper.orderByDesc(User::getId);
        
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public User getUserByLoginname(String loginname) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getLoginname, loginname);
        return this.getOne(queryWrapper);
    }

    @Override
    public User getUserByRealname(String realname) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getRealname, realname);
        return this.getOne(queryWrapper);
    }
} 